package br.com.maps.ui;

import java.net.MalformedURLException;
import java.net.URL;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.widget.TabHost.OnTabChangeListener;
import br.com.map.data.DataObtainerMock;
import br.com.maps.data.DataFetcher;
import br.com.maps.fragments.ContacDataListener;
import br.com.maps.fragments.ContactsListFragment;
import br.com.maps.fragments.FavouriteListFragment;
import br.com.maps.ramais.R;

public class MapsRamaisActivity extends FragmentActivity implements ContacDataListener {

	private ContactsListFragment contactsFragment;
	public FavouriteListFragment favouriteFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.createContactsFragment();
		this.createFavouriteFragment();
		this.setContentView(R.layout.maps_ramais_activity);
		DataFetcher fetcher = new DataFetcher(this, new DataObtainerMock());
		try {
			fetcher.execute(new URL("http://www.gooogle.com.br"));
		} catch (MalformedURLException e) {
			Log.println(Log.ERROR, "ramais", e.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.getMenuInflater().inflate(R.menu.maps_ramais, menu);
		return true;
	}

	@Override
	public void dataSetChanged() {
		this.contactsFragment.dataSetChanged();
		this.favouriteFragment.dataSetChanged();
	}

	private void createContactsFragment() {
		MapsRamaisActivity.this.contactsFragment = new ContactsListFragment();
		MapsRamaisActivity.this.contactsFragment.setActivity(MapsRamaisActivity.this);
	}

	private void createFavouriteFragment() {
		MapsRamaisActivity.this.favouriteFragment = new FavouriteListFragment();
		MapsRamaisActivity.this.favouriteFragment.setActivity(MapsRamaisActivity.this);
	}

	public class TabChangedListener implements OnTabChangeListener {

		@Override
		public void onTabChanged(String tabId) {
			FragmentManager fm = MapsRamaisActivity.this.getSupportFragmentManager();
			ContactsListFragment contactsFragment = (ContactsListFragment) fm.findFragmentByTag("contacts");
			FavouriteListFragment favouritesFragment = (FavouriteListFragment) fm.findFragmentByTag("favoritos");
			FragmentTransaction transaction = fm.beginTransaction();
			if (contactsFragment != null) {
				transaction.detach(contactsFragment);
			}
			if (favouritesFragment != null) {
				transaction.detach(favouritesFragment);
			}
			if (tabId.equalsIgnoreCase("contacts")) {
				if (contactsFragment == null) {
					transaction.add(R.id.realtabcontent, MapsRamaisActivity.this.contactsFragment, "contacts");
				} else {
					transaction.attach(contactsFragment);
				}
			} else if (tabId.equalsIgnoreCase("favourites")) {
				if (favouritesFragment == null) {
					transaction.add(R.id.realtabcontent, MapsRamaisActivity.this.favouriteFragment, "favoritos");
				} else {
					transaction.attach(favouritesFragment);
				}
			}
			transaction.commit();
		}

	}

}
